<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@page import="java.sql.*"%>
<%@page import="noname.*"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>User Registration/Information</title>
<style type="text/css">
.errors {
	padding-left: 50px;
}
</style>
<STYLE TYPE="text/css">
a:visited {
	color: green
}

a:active {
	color: blue
}

a:hover {
	color: magenta
}
</STYLE>
<link href="css/style.css" rel="stylesheet" type="text/css" />
</head>
<body style="margin-top: 0; margin-left: 0; background-color: white">
<div align="center">
<table width="800" style="border: 3px solid #196f99;" cellspacing="0"
	cellpadding="0" rules="none">
	<tr>
		<td><!--#054463 font-face:verdana; font-size:12pt; --> <img
			src="Wayden.jpg" align="left" HSPACE=0></td>
	</tr>
	<tr>
		<td><!--#054463 font-face:verdana; font-size:12pt; -->
		<table width="auto" height="auto" border="0" cellspacing="0"
			cellpadding="0" rules="none">
			<tr>
				<td colspan=2><!--#054463 font-face:verdana; font-size:12pt; -->
				<table width="180" height=700 style="background-color: #ebf3fd;">
					<tr>
						<td height="50" width="180" bgcolor="#339999">&nbsp;</td>
					</tr>
					<tr>
						<td colspan=2>&nbsp;</td>
					</tr>
					<tr>
						<td>
						<form name="form1" action="login.jsp" method="post"
							onSubmit="return Validate()">
						<table align="center">
							<tr>
								<td>
								<h2>Login Authentification</h2>
								</td>
							</tr>
						</table>
						<table width="180" align="center"
							style="background-color: #ebf3fd;">
							<tr>
								<td><b>Login Name</b></td>
								<td><input type="text" name="userName" value="" style=""
									width="30"></td>
							</tr>
							<tr>
								<td><b>Password</b></td>
								<td><input type="password" name="password" value=""
									style="" width="30"></td>
							</tr>

							<tr>
								<td></td>
								<td><input type="submit" name="Submit" value="SignIn"
									style="" align="left"></td>
							</tr>
							<!--<tr><td colspan=2>&nbsp;</td></tr>-->
						</table>
						<table>
							<tr>
								<td>
								<p align="left"><font
									style="font-family: arial, tahoma; font-size: 10px;"> <i>New
								User?</i> </font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
								&nbsp; <font
									style="font-family: arial, verdana; font-size: 12px"> <a
									href="Registration.html">Register Here</a> </font>
								</td>
							</tr>
						</table>
						</form>
						</td>
					</tr>
					<tr>
						<td height="650" width="180" bgcolor="#339999">&nbsp;</td>
					</tr>
				</table>
				</td>
				<td height="700" width="700"><!--#054463 font-face:verdana; font-size:12pt; -->
				<form name="frm" action="registration.jsp" method="post"
					onSubmit="return Validate()">
				<table align="center" width="500">
					<tr>
						<td>
						<h2>Registration User</h2>
						</td>
					</tr>
				</table>
				<table width="auto" align="center"
					style="background-color: #ebf3fd;">
					<tr>
						<td colspan=2></td>
					</tr>
					<tr>
						<td colspan=2>&nbsp;</td>
					</tr>
					<tr>
						<td><b>Fullname</b></td>
						<td><input type="text" name="name" width="30"></td>
					</tr>
					<tr>
						<td><b>User Name</b></td>
						<td><input type="text" name="login" width="30"></td>
					</tr>
					<tr>
						<td><b>Password</b></td>
						<td><input type="text" name="password" width="30"></td>
					</tr>


					<tr>
						<td><b>Email</b></td>
						<td><input type="text" name="email" width="30"></td>
					</tr>
					<tr>
						<td></td>
						<td><input type="submit" name="Submit" value="Submit"
							align="left"></td>
					</tr>

				</table>
				<%
					String INS = "INSERT INTO users (id_role,fullname,login,password,email,activated) VALUES (?,?,?,?,?,0);";
					String fullname = request.getParameter("name");
					String login = request.getParameter("login");
					String password = request.getParameter("password");
					String email = request.getParameter("email");
					String select = "select * from users order by id desc";
					String insert = "insert into profile (id_user,about,logo) values (?,?,?)";
					String check = "select * from users where login = ?";
					Connection connection = null;
					ConnectDB connectDB = new ConnectDB();

					PreparedStatement pstatement = null;
					if (fullname != null && login != null && password != null
							&& email != null) {

						try {
							connection = connectDB.Connect();

							Statement st = connection.createStatement();
							PreparedStatement ps = connection.prepareStatement(check);
							ps.setString(1, login);
							ps.execute();
							ResultSet rs = ps.getResultSet();
							rs.next();
							int isRegistered = rs.getRow();

							if (isRegistered == 0) {
								ps = connection.prepareStatement(INS);
								ps.setInt(1, 1);
								ps.setString(2, fullname);
								ps.setString(3, login);
								ps.setString(4, password);
								ps.setString(5, email);
								int count = ps.executeUpdate();
								if (count > 0) {

									st.executeQuery(select);
									rs = st.getResultSet();
									rs.next();
									String id = rs.getString(1);

									ps = connection.prepareStatement(insert);
									ps.setString(1, id);
									ps.setString(2, "");
									ps.setString(3, "");

									ps.execute();

								}
								response.sendRedirect("login.jsp");
							} else {
								response.sendRedirect("registration.jsp");
							}

							connection.close();
						} catch (SQLException ex) {
							ex.printStackTrace();
							out.println("Unable to connect to batabase.");
						}

					}
				%>
				</form>
				<table align="center">
					<tr>
						<td height="700">&nbsp;</td>
					</tr>
				</table>
				</td>
				</td>
			</tr>
		</table>
		</td>
	</tr>
</table>
<table>
	<tr>
		<td width="800" height="20">
		<p align="center"><font
			style="font-family: tahoma; font-size: 10px;"> <i>&copy;
		Copyright training Антон Lucky Cat, Лиходед Сашка, Микола и Тарас
		Одинский </i> </font>
		</td>
	</tr>
</table>
</body>
</html>